package com.ld.hpfs.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ld.hpfs.pojo.User;

public class WriterPrintDataForXML {
	public static boolean writerDataForXML(List<HashMap<String,Object>> list,User user,File prePrintDataFile)throws IOException{
		//向xml文件中写入要打印的数据

			File prePrintDataFile1 = prePrintDataFile;
			OutputStreamWriter writers = new OutputStreamWriter(new FileOutputStream(prePrintDataFile1), "UTF-8");
			//把要打印的数据写入XML文件中
			writers.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
			writers.write("\r\n");
			writers.write("<PrintData>");writers.write("\r\n");
			for(HashMap<String,Object> info :list){
		        writers.write("	<Invoice>");writers.write("\r\n");        
		        
		        /*获得head信息并写入文件*/
		        HashMap<String,Object> headInfo = (HashMap<String,Object>)info.get("headInfo");
		        Date date = (Date)headInfo.get("DOCUMENT_DATE");
		        String DOCUMENT_DATE = "";
		        if(date ==null){
		        	DOCUMENT_DATE = "1900-01-01";
		        }else{
		        	DOCUMENT_DATE = new SimpleDateFormat("yyyyMMdd").format(date);
		        }
		        
		        String DOC_TYPE_CODE = (String)headInfo.get("DOC_TYPE_CODE");
		        if(DOC_TYPE_CODE.equalsIgnoreCase("VAT")){//专票
		        	DOC_TYPE_CODE = "增票";
		        }else{//普票
		        	DOC_TYPE_CODE = "普票";
		        }
		        Integer PRICE_WAY = (Integer)headInfo.get("PRICE_WAY");//计价方式：1含税,0不含税
		        if(PRICE_WAY==null ){
		        	PRICE_WAY = 0;
		        }
		        String CUST_CODE = (String)headInfo.get("CUST_CODE");//客户编号
		        if(CUST_CODE==null || CUST_CODE.isEmpty()){
		        	CUST_CODE = "0";
		        }
		        
		        Date DATE = (Date)headInfo.get("INVOICE_DATE");
		        String INVOICE_DATE = "";
		        if(DATE==null ){
		        	INVOICE_DATE = "19000101";
		        }else{
		        	INVOICE_DATE = new SimpleDateFormat("yyyyMMdd").format(DATE);//票据日期
		        }
		        
		        String CUST_NAME_CHN = (String)headInfo.get("CUST_NAME_CHN");
		        if(CUST_NAME_CHN==null || CUST_NAME_CHN.isEmpty()){
		        	CUST_NAME_CHN = "";
		        }
		        
		        String TAX_CODE = (String)headInfo.get("TAX_CODE");
		        if(TAX_CODE==null || TAX_CODE.isEmpty()){
		        	TAX_CODE = "";
		        }
		        
		        String ADDRESS = (String)headInfo.get("ADDRESS");
		        if(ADDRESS==null || ADDRESS.isEmpty()){
		        	ADDRESS = "";
		        }
		        
		        String BANK_ACCOUNT = (String)headInfo.get("BANK_ACCOUNT");
		        if(BANK_ACCOUNT==null || BANK_ACCOUNT.isEmpty()){
		        	BANK_ACCOUNT = "";
		        }
		        
		        String SELLER_ADDRESS = (String)headInfo.get("SELLER_ADDRESS");
		        if(SELLER_ADDRESS==null || SELLER_ADDRESS.isEmpty()){
		        	SELLER_ADDRESS = "";
		        }
		        
		        String SELLER_BANK_ACCOUNT = (String)headInfo.get("SELLER_BANK_ACCOUNT");
		        if(SELLER_BANK_ACCOUNT==null || SELLER_BANK_ACCOUNT.isEmpty()){
		        	SELLER_BANK_ACCOUNT = "";
		        }
		        
		        String REMARK = (String)headInfo.get("REMARK");
		        if(REMARK==null || REMARK.isEmpty()){
		        	REMARK = "";
		        }
		        
		        String OPERATOR = (String)headInfo.get("OPERATOR");
		        if(OPERATOR==null || OPERATOR.isEmpty()){
		        	OPERATOR = "";
		        }
		        
		        String CHECKER = (String)headInfo.get("CHECKER");
		        if(CHECKER==null || CHECKER.isEmpty()){
		        	CHECKER = "";
		        }
		        
		        String REMITTEE = (String)headInfo.get("REMITTEE");
		        if(REMITTEE==null || REMITTEE.isEmpty()){
		        	REMITTEE = "";
		        }
		        
		        String GOODS_LIST_NAME = (String)headInfo.get("GOODS_LIST_NAME");
		        if(GOODS_LIST_NAME==null || GOODS_LIST_NAME.isEmpty()){
		        	GOODS_LIST_NAME = "";
		        }
		        
		        String INVOICE_CODE = (String)headInfo.get("INVOICE_CODE");
		        if(INVOICE_CODE==null || INVOICE_CODE.isEmpty()){
		        	INVOICE_CODE = "";
		        }
		        
		        String INVOICE_NO = (String)headInfo.get("INVOICE_NO");
		        if(INVOICE_NO==null || INVOICE_NO.isEmpty()){
		        	INVOICE_NO = "";
		        }
		        
		        String REF01 = (String)headInfo.get("REF01");
		        if(REF01==null || REF01.isEmpty()){
		        	REF01 = "";
		        }
		        String REF02 = (String)headInfo.get("REF02");
		        if(REF02==null || REF02.isEmpty()){
		        	REF02 = "";
		        }
		        String REF03 = (String)headInfo.get("REF03");
		        if(REF03==null || REF03.isEmpty()){
		        	REF03 = "";
		        }
		        String REF04 = (String)headInfo.get("REF04");
		        if(REF04==null || REF04.isEmpty()){
		        	REF04 = "";
		        }
		        String REF05 = (String)headInfo.get("REF05");
		        if(REF05==null || REF05.isEmpty()){
		        	REF05 = "";
		        }
		    	writers.write("		<Header>");writers.write("\r\n");        	        	       	
				writers.write("			<HeadCode>"+headInfo.get("ID_HEAD_ID")+"</HeadCode>");writers.write("\r\n");
				writers.write("			<DocDate>"+DOCUMENT_DATE+"</DocDate>");writers.write("\r\n");
				writers.write("			<InvSort>"+DOC_TYPE_CODE+"</InvSort>");writers.write("\r\n");
				writers.write("			<CustCode>"+CUST_CODE+"</CustCode>");writers.write("\r\n");
				writers.write("			<CustName>"+CUST_NAME_CHN+"</CustName>");writers.write("\r\n");
				writers.write("			<CustTaxNo>"+TAX_CODE+"</CustTaxNo>");writers.write("\r\n");
				writers.write("			<CustAdress>"+ADDRESS+"</CustAdress>");writers.write("\r\n");
				writers.write("			<CustBankAccount>"+BANK_ACCOUNT+"</CustBankAccount>");writers.write("\r\n");
				writers.write("			<SellerAddress>"+SELLER_ADDRESS+"</SellerAddress>");writers.write("\r\n");
				writers.write("			<SellerBankAccount>"+SELLER_BANK_ACCOUNT+"</SellerBankAccount>");writers.write("\r\n");
				writers.write("			<Remark>"+REMARK+"</Remark>");writers.write("\r\n");
				writers.write("			<Printer>"+OPERATOR+"</Printer>");writers.write("\r\n");
				writers.write("			<Review>"+CHECKER+"</Review>");writers.write("\r\n");
				writers.write("			<Receivable>"+REMITTEE+"</Receivable>");writers.write("\r\n");
				writers.write("			<PriceWay>"+PRICE_WAY+"</PriceWay>");writers.write("\r\n");
				writers.write("			<InventoryGoodsName>"+GOODS_LIST_NAME+"</InventoryGoodsName>");writers.write("\r\n");
				writers.write("			<InvoiceCode>"+INVOICE_CODE+"</InvoiceCode>");writers.write("\r\n");
				writers.write("			<InvoiceNo>"+INVOICE_NO+"</InvoiceNo>");writers.write("\r\n");
				writers.write("			<InvoiceDate>"+INVOICE_DATE+"</InvoiceDate>");writers.write("\r\n");
				writers.write("			<Ref01>"+REF01+"</Ref01>");writers.write("\r\n");
				writers.write("			<Ref02>"+REF02+"</Ref02>");writers.write("\r\n");
				writers.write("			<Ref03>"+REF03+"</Ref03>");writers.write("\r\n");
				writers.write("			<Ref04>"+REF04+"</Ref04>");writers.write("\r\n");
				writers.write("			<Ref05>"+REF05+"</Ref05>");writers.write("\r\n");
				
				writers.write("		</Header>");writers.write("\r\n");	        	
		       
		    	/*获得detail信息并写入文件*/ 
				List<HashMap<String,Object>> deatialInfoList = (List<HashMap<String,Object>>)info.get("deatialInfo");
				
				for(HashMap<String,Object> deatial:deatialInfoList){
					Integer GOODS_LINE = (Integer)deatial.get("GOODS_LINE");//商品行数
					if(GOODS_LINE==null ){
						GOODS_LINE = 0;
					}
					String GOODS_CODE = (String)deatial.get("GOODS_CODE");//客户代码
					if(GOODS_CODE==null ||GOODS_CODE.isEmpty()){
						GOODS_CODE = "0";
					}
					String PRICE_CUR = (String)deatial.get("PRICE_CUR").toString();//本币单价
					if(PRICE_CUR==null || PRICE_CUR.isEmpty()){
						PRICE_CUR = "0";
					}
					String QUANTITY = (String)deatial.get("QUANTITY").toString();//数量
					if(QUANTITY==null || QUANTITY.isEmpty()){
						QUANTITY = "0";
					}
					String AMOUNT_CUR = (String)deatial.get("AMOUNT_CUR").toString();//本币金额
					if(AMOUNT_CUR==null || AMOUNT_CUR.isEmpty()){
						AMOUNT_CUR = "0";
					}
					String TAX_RATE = (String)deatial.get("TAX_RATE").toString();//税率
					if(TAX_RATE==null || TAX_RATE.isEmpty()){
						TAX_RATE = "0";
					}
					String TAX_AMOUNT = (String)deatial.get("TAX_AMOUNT").toString();//税额
					if(TAX_AMOUNT==null || TAX_AMOUNT.isEmpty()){
						TAX_AMOUNT = "0";
					}
					
					String GOODS_NAME = (String)deatial.get("GOODS_NAME");
					if(GOODS_NAME==null || GOODS_NAME.isEmpty()){
						GOODS_NAME = "";
					}
					
					String SPEC = (String)deatial.get("SPEC");
					if(SPEC==null || SPEC.isEmpty()){
						SPEC = "";
					}
					
					String GOODS_UNIT = (String)deatial.get("GOODS_UNIT");
					if(GOODS_UNIT==null || GOODS_UNIT.isEmpty()){
						GOODS_UNIT = "";
					}
					String TAX_ITEM = (String)deatial.get("TAX_ITEM");
					if(TAX_ITEM==null || TAX_ITEM.isEmpty()){
						TAX_ITEM = "";
					}
					
					String REMARKdetail = (String)deatial.get("REMARK");
					if(REMARKdetail==null || REMARKdetail.isEmpty()){
						REMARKdetail = "";
					}
					
					String REF01detail = (String)deatial.get("REF01");
					if(REF01detail==null || REF01detail.isEmpty()){
						REF01detail = "";
					}
					
					String REF02detail = (String)deatial.get("REF02");
					if(REF02detail==null || REF02detail.isEmpty()){
						REF02detail = "";
					}
					String REF03detail = (String)deatial.get("REF03");
					if(REF03detail==null || REF03detail.isEmpty()){
						REF03detail = "";
					}
			        writers.write("		<Detail>");writers.write("\r\n");     			
			        writers.write("			<GoodsLine>"+GOODS_LINE+"</GoodsLine>");writers.write("\r\n");
			        writers.write("			<GoodCode>"+GOODS_CODE+"</GoodCode>");writers.write("\r\n");
			        writers.write("			<GoodName>"+GOODS_NAME+"</GoodName>");writers.write("\r\n");
			        writers.write("			<Spec>"+SPEC+"</Spec>");writers.write("\r\n");
			        writers.write("			<Unit>"+GOODS_UNIT+"</Unit>");writers.write("\r\n");
			        writers.write("			<TaxItems>"+TAX_ITEM+"</TaxItems>");writers.write("\r\n");
			        writers.write("			<Price>"+PRICE_CUR+"</Price>");writers.write("\r\n");
			        writers.write("			<Quantity>"+QUANTITY+"</Quantity>");writers.write("\r\n");
			        writers.write("			<Amount>"+AMOUNT_CUR+"</Amount>");writers.write("\r\n");
			        writers.write("			<TaxRate>"+TAX_RATE+"</TaxRate>");writers.write("\r\n");
			        writers.write("			<TaxAmount>"+TAX_AMOUNT+"</TaxAmount>");writers.write("\r\n");
			        writers.write("			<DiscountLineCount>"+"0"+"</DiscountLineCount>");writers.write("\r\n");
			        writers.write("			<Remark>"+REMARKdetail+"</Remark>");writers.write("\r\n");
			        writers.write("			<Ref01>"+REF01detail+"</Ref01>");writers.write("\r\n");
			        writers.write("			<Ref02>"+REF02detail+"</Ref02>");writers.write("\r\n");
			        writers.write("			<Ref03>"+REF03detail+"</Ref03>");writers.write("\r\n");
			        writers.write("		</Detail>");writers.write("\r\n");				
				}	    		    		    		
		        writers.write("	</Invoice>");writers.write("\r\n");
			}

	        
			writers.write("</PrintData>");
			
			writers.flush();
	        writers.close();
		
		return true;	
	}
}
